package com.living.filedownloader.ui.adapter;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.liulishuo.filedownloader.model.FileDownloadStatus;
import com.living.R;
import com.living.utils.DownLoadUtils;

import org.jetbrains.annotations.NotNull;

public class TaskItemViewHolder extends BaseViewHolder {

    public final static int callbackProgressTimes = 1000;

    /**
     * download id
     */
    public int taskid;
    public int downloadStatus;

    public View root_download;
    public TextView taskNameTv;
    public TextView taskStatusTv;
    public ProgressBar taskPb;
    TextView task_status_speed;
    TextView task_status_size;
    ImageView task_action_more;

    public TaskItemViewHolder(@NotNull View view) {
        super(view);
        assignViews();
    }

    public void update(final int taskid) {
        this.taskid = taskid;
    }

    /***
     * 下载完毕
     */
    public void updateDownloaded() {
        downloadStatus = FileDownloadStatus.completed;
        taskPb.setMax(1);
        taskPb.setProgress(1);
//        taskStatusTv.setText(R.string.tasks_manager_demo_status_completed);
        taskStatusTv.setText("作者");
        viewShowCase();
    }

    /***
     * 下载中
     */
    public void updateNotDownloaded(final int status, final long sofar, final long total) {
        if (sofar > 0 && total > 0) {
            final float percent = sofar
                    / (float) total;
            taskPb.setMax(100);
            taskPb.setProgress((int) (percent * 100));
        } else {
            taskPb.setMax(1);
            taskPb.setProgress(0);
        }
        downloadStatus = status;
        switch (status) {
            case FileDownloadStatus.error:
                taskStatusTv.setText(R.string.tasks_manager_demo_status_error);
                break;
            case FileDownloadStatus.paused:
                taskStatusTv.setText(R.string.tasks_manager_demo_status_paused);
                break;
            default:
                taskStatusTv.setText(R.string.tasks_manager_demo_status_paused);
                break;
        }
        viewShowCase();
    }

    long lastSofar;

    /***
     * 更新下载状态
     * @param status
     * @param sofar
     * @param total
     */
    public void updateDownloading(final int status, final long sofar, final long total) {
        final float percent = sofar / (float) total;
        taskPb.setMax(100);
        taskPb.setProgress((int) (percent * 100));
        downloadStatus = status;
        switch (status) {
            case FileDownloadStatus.pending:
                taskStatusTv.setText(R.string.tasks_manager_demo_status_pending);
                break;
            case FileDownloadStatus.connected:
            case FileDownloadStatus.started:
                taskStatusTv.setText(R.string.tasks_manager_demo_status_started);
                break;
            case FileDownloadStatus.progress:
                taskStatusTv.setText(R.string.tasks_manager_demo_status_progress);
                task_status_speed.setText(DownLoadUtils.byte2FitMemorySize((sofar - lastSofar) / callbackProgressTimes) + "/S");
                task_status_size.setText(DownLoadUtils.byte2FitMemorySize(sofar) + "/" + DownLoadUtils.byte2FitMemorySize(total));
                lastSofar = sofar;
                break;
            default:
                taskStatusTv.setText(R.string.tasks_manager_demo_status_progress);
                break;
        }
        viewShowCase();
    }

    /***
     * ui  的显示\隐藏
     */
    private void viewShowCase() {
        switch (downloadStatus) {
            case FileDownloadStatus.connected:
            case FileDownloadStatus.started:
            case FileDownloadStatus.progress: {
                taskStatusTv.setVisibility(View.GONE);
                taskPb.setVisibility(View.VISIBLE);
                task_status_speed.setVisibility(View.VISIBLE);
                task_status_size.setVisibility(View.VISIBLE);
            }
            break;
            case FileDownloadStatus.INVALID_STATUS:
            case FileDownloadStatus.pending:
            case FileDownloadStatus.error:
            case FileDownloadStatus.paused: {
                //
                taskStatusTv.setVisibility(View.VISIBLE);
                taskPb.setVisibility(View.GONE);
                task_status_speed.setVisibility(View.GONE);
                task_status_size.setVisibility(View.GONE);
                if (downloadStatus == FileDownloadStatus.error) {
                    taskStatusTv.setTextColor(Color.RED);
                } else {
                    taskStatusTv.setTextColor(Color.parseColor("#8C8C8C"));
                }
            }
            break;
            case FileDownloadStatus.completed: {
                taskStatusTv.setVisibility(View.VISIBLE);
                taskPb.setVisibility(View.GONE);
                task_status_speed.setVisibility(View.GONE);
                task_status_size.setVisibility(View.GONE);
            }
            break;
            default:
                break;
        }
    }

    private void assignViews() {
        root_download = getView(R.id.root_download);

        taskNameTv = getView(R.id.task_name_tv);
        taskStatusTv = getView(R.id.task_status_tv);
        taskPb = getView(R.id.task_pb);
        task_status_speed = getView(R.id.task_status_speed);
        task_status_size = getView(R.id.task_status_size);
        task_action_more = getView(R.id.task_action_more);
    }

}
